package org.lsda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lsda.job.LsdaAction;
import org.lsda.sched.LsdaScheduler;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * Servlet implementation class ListAction
 */
@WebServlet("/ListAction")
public class ListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(ListAction.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String jobName = request.getParameter("JobName");
		String groupName = request.getParameter("JobGroup");
		log.info(jobName);
		log.info(groupName);
		
		ArrayList<LsdaAction> actions = null;
		JsonObject json = new JsonObject();
		json.addProperty("Result", "OK");
		try {
			actions = LsdaScheduler.listActions(groupName, jobName);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", e.getMessage());
			log.fatal(e.getCause());
			PrintWriter writer = response.getWriter();
//			System.out.println(json.toString());
			writer.write(json.toString());
			return ;
		}
		int total = 0;
		// TODO Auto-generated method stub
		
		JsonArray records = new JsonArray();
		if (actions != null) {
			for (LsdaAction act : actions) {
				// for(String v:jd.getJobDataMap().getKeys())
				// System.out.println(v);
				JsonObject record = new JsonObject();
				record.addProperty("JobGroup", groupName);
				record.addProperty("JobName", jobName);
				record.addProperty("ActionSeq", act.getActionSeq());
				record.addProperty("Command", act.getPrgName().trim());
				record.addProperty("Parameter", act.getPrgParam());
				record.addProperty("ActDesc", act.getInfo());
				records.add(record);
				total++;
			}

		}
		
		json.add("Records", records);
		json.addProperty("TotalRecordCount", total);
		PrintWriter writer = response.getWriter();
//		System.out.println(json.toString());
		writer.write(json.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
